package scau.hotel.hotel_service.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import scau.hotel.hotel_service.entity.RoomType;
import scau.hotel.hotel_service.mapper.RoomTypeMapper;
import scau.hotel.hotel_service.service.RoomTypeService;
import scau.hotel.hotel_model.controller.RespBean;
import scau.hotel.hotel_model.controller.RespBeanEnum;
import scau.hotel.hotel_service.vo.RoomTypeVo;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (RoomType)表服务实现类
 *
 * @author makejava
 * @since 2022-03-03 22:46:34
 */
@Service("roomTypeService")
public class RoomTypeServiceImpl implements RoomTypeService {
    @Resource
    private RoomTypeMapper roomTypeMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param roomTypeId 主键
     * @return 实例对象
     */
    @Override
    public RoomType queryById(Integer roomTypeId) {
        return this.roomTypeMapper.queryById(roomTypeId);
    }

    /**
     * 分页查询
     *
     * @param roomType 筛选条件
     * @return 查询结果
     */
    @Override
    public List<RoomType> queryAll(RoomType roomType) {
        return this.roomTypeMapper.queryAll(roomType);
    }

    /**
     * 分页查询
     *
     * @param roomType    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<RoomType> queryByPage(RoomType roomType, PageRequest pageRequest) {
        long total = this.roomTypeMapper.count(roomType);
        return new PageImpl<>(this.roomTypeMapper.queryAllByLimit(roomType, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param roomTypeVo 实例对象
     * @return 实例对象
     */
    @Override
    public RespBean insert(RoomTypeVo roomTypeVo) {
        if(null == this.roomTypeMapper.queryRoomTypeByRoomTypeNo(roomTypeVo.getRoomTypeNo())) {
            this.roomTypeMapper.insert(roomTypeVo);
            return RespBean.success();
        }else{
            return RespBean.error(RespBeanEnum.ROOM_TYPE_NO_EXIST_ERROR);
        }
    }

    /**
     * 修改数据
     *
     * @param roomTypeVo 实例对象
     * @return 实例对象
     */
    @Override
    public RespBean update(RoomTypeVo roomTypeVo) {
        if (null == this.roomTypeMapper.queryRoomTypeByRoomTypeNo(roomTypeVo.getRoomTypeNo())) {
            this.roomTypeMapper.update(roomTypeVo);
            return RespBean.success();
        }
        if(Objects.equals(roomTypeVo.getRoomTypeNo(), this.roomTypeMapper.queryById(roomTypeVo.getRoomTypeId()).getRoomTypeNo())){
            this.roomTypeMapper.update(roomTypeVo);
            return RespBean.success();
        }
        else {
            return RespBean.error(RespBeanEnum.ROOM_TYPE_NO_EXIST_ERROR);
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param roomTypeId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer roomTypeId) {
        return this.roomTypeMapper.deleteById(roomTypeId) > 0;
    }

}
